package com.shop.admin.service.impl;

import com.shop.Result.LdbzResult;
import com.shop.admin.service.AdminSearchLeftAdService;
import com.shop.api.feign.SearchLeftAdFeign;
import com.shop.pojo.LdbzSearchLeftAd;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@GlobalTransactional
public class AdminSearchLeftAdServiceImpl implements AdminSearchLeftAdService {

    private static final Logger logger = LoggerFactory.getLogger(AdminSearchLeftAdServiceImpl.class);

    @Autowired
    private SearchLeftAdFeign searchLeftAdFeign;

    @Override
    public LdbzResult countSearchLeftAd(LdbzSearchLeftAd entity) {
        return LdbzResult.ok(searchLeftAdFeign.countByEntity(entity));
    }

    @Override
    public LdbzResult getSearchLeftAdPage(LdbzSearchLeftAd entity, int pn, int limit) {
        Map<String,Object> map = new HashMap<String,Object>();
        long total = searchLeftAdFeign.countByEntity(entity);
        map.put("total", total) ;
        if(total>0 && pn>0) {
            int start = (pn-1)*limit ;
            List<LdbzSearchLeftAd> ret = searchLeftAdFeign.getSearchLeftAdPage(entity, start, limit);
            map.put("list", ret) ;
        }
        return LdbzResult.build(0, "", map);
    }

    @Override
    public LdbzResult selectByKey(Long id) {
        return LdbzResult.ok(searchLeftAdFeign.selectByKey(id));
    }

    @Override
    public LdbzResult deleteByKey(String id) {
        if(StringUtils.contains(id, ",")) {
            List<Long> ids = new ArrayList<Long>();
            for(String _id : StringUtils.split(id , ",")) {
                ids.add(Long.valueOf(_id));
            }
            return LdbzResult.ok(searchLeftAdFeign.deleteByKeys(ids));
        }else {
            return LdbzResult.ok(searchLeftAdFeign.deleteByKey(Long.valueOf(id)));
        }
    }

    @Override
    public LdbzResult insertByEntity(LdbzSearchLeftAd entity) {
        if(searchLeftAdFeign.insertByEntity(entity)>=1) {
            return LdbzResult.ok();
        }else {
            return LdbzResult.build(500, "添加失败");
        }
    }

    @Override
    public LdbzResult updateByKey(LdbzSearchLeftAd entity) {
        return LdbzResult.ok(searchLeftAdFeign.updateByKey(entity));
    }
}
